package com.fywsy.fyweb.repository;

import com.fywsy.fyweb.entity.Category;
import com.fywsy.fyweb.entity.Item;
import com.fywsy.fyweb.entity.projection.ItemProjection;
import jakarta.transaction.Transactional;
import org.springframework.data.domain.Example;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

@Repository
public interface ItemRepository extends JpaRepository<Item,Long> {


    List<ItemProjection> findAllByEnableAndGameIdAndGongnengId(boolean enable, long gameId, long gongnengId);

    Item findFirstByGameIdAndGongnengIdAndC(long gameId, long gongnengId,String c);

    List<Item> findAllByGameIdAndNContaining(Long gameId, String name);


    @Modifying
    @Transactional
    @Query(value = "DELETE FROM item WHERE game_id = :game_id AND gongneng_id NOT IN :ids", nativeQuery = true)
    void deleteByGameIdAndGongnengIdNotIn(@Param("game_id") long gameId, @Param("ids") Set<Long> ids);

    @Modifying
    @Transactional
    @Query(value = "DELETE FROM item WHERE game_id = :game_id", nativeQuery = true)
    void deleteByGameId(@Param("game_id") long gameId);


    @Query(value = "SELECT COUNT(id) FROM item u WHERE u.game_id = :game_id and u.gongneng_id = :gongneng_id", nativeQuery = true)
    long countByGameIdAndGongnengId(@Param("game_id") long gameId, @Param("gongneng_id") long gongnengId);



}